// pages/Fl_Index2/index.js
Page({

  /**
   * 页面的初始数据
   */
	data: {

		userInfo: {},
		hasUserInfo: true,
		canIUse: wx.canIUse('button.open-type.getUserInfo'),

		large_swiper_autoplay:false,
		swiper_index: '1',
		swiper_length: '1',
	},

  /**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {
		var scene = ''
		if (options.scene) {
			scene = decodeURIComponent(options.scene)
		}

		this.requestNetGetJSPIOpenID(scene);
		this.requestNetHomeButton();
		this.requestNetHomeADLarge();
		this.requestNetHomeAD();
		this.requestNetHomeProductList();
	},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function () {

	},

  /**
   * 生命周期函数--监听页面显示
   */
	onShow: function () {
		this.requestNetHomeProductList();
		var value = wx.getStorageSync('HasUserInfo');
		if (value != true) {
			this.setData({
				hasUserInfo: false
			})
		} else {
			this.setData({
				hasUserInfo: true
			})
		}
	},

  /**
   * 生命周期函数--监听页面隐藏
   */
	onHide: function () {

	},

  /**
   * 生命周期函数--监听页面卸载
   */
	onUnload: function () {

	},

	/**
	 * 监听事件————轮播图监听
	 */
	bindEventSwiper: function (res) {
		this.setData({
			swiper_index: res.detail.currentItemId
		})
	},

	/**
	 * 网络请求--用户登录并获取Token
	 */
	requestNetGetJSPIOpenID: function (scene) {
		wx.login({
			success: res => {
				if (res.code) {
					wx.request({
						url: 'https://www.chnode.xyz/api/Other.ashx',
						data: {
							'DataType': 'GetJSPIOpenID',
							'js_code': res.code,
							'FUserID': scene
						},
						success: function (e) {
							if (e.data.isok == 'true') {
								getApp().data.openid = e.data.data.openid
								getApp().data.Token = e.data.data.Token
							} else {
								wx.showModal({
									title: '提示',
									content: e.data.info,
									showCancel: false,
								})
							}
						},
						fail: function (e) {
						}
					})
				}
			}
		})
	},

	/**
 * 网络请求--首页轮播  大轮播
 */
	requestNetHomeADLarge: function () {
		var _this = this;
		wx.showNavigationBarLoading();
		wx.request({
			url: 'https://www.chnode.xyz/api/Home.ashx?DataType=Home_AD_AL',
			success: function (res) {
				console.log(res)
				if (res.data.isok == "true") {

					_this.setData({
						Home_AD_ALs: res.data.datas,
						large_swiper_autoplay: res.data.datas.length!=1
					});
				}
			},
			complete: function () {
				wx.hideNavigationBarLoading()
			}
		})
	},

	/**
	 * 网络请求--首页轮播
	 */
	requestNetHomeAD: function () {
		var _this = this;
		wx.showNavigationBarLoading();
		wx.request({
			url: 'https://www.chnode.xyz/api/Home.ashx?DataType=Home_AD',
			success: function (res) {
				if (res.data.isok == "true") {
					_this.setData({
						HomeADs: res.data.datas,
						swiper_length: res.data.datas.length
					});
				}
			},
			complete: function () {
				wx.hideNavigationBarLoading()
			}
		})
	},
	/**
	 * 网络请求--首页导航，四个按钮
	 */
	requestNetHomeButton: function () {
		var _this = this;
		wx.showNavigationBarLoading();
		wx.request({
			url: 'https://www.chnode.xyz/api/Home.ashx?DataType=Home_Button',
			success: function (res) {
				console.log(res)
				if (res.data.isok == "true") {
					_this.setData({
						HomeButtons: res.data.datas,
					});
				}
			},
			complete: function () {
				wx.hideNavigationBarLoading()
			}
		})
	},
	/**
 * 网络请求--商品列表
 */
	requestNetHomeProductList: function () {
		var _this = this;
		wx.showNavigationBarLoading();
		wx.request({
			url: 'https://www.chnode.xyz/api/Home.ashx?DataType=Home_ProductList',
			data: {
				AS_ID: getApp().data.AS_ID,
				A_ID: getApp().data.A_ID,
			},
			success: function (res) {
				if (res.data.isok == "true" && res.data.datas != null && res.data.datas.length > 0) {
					var temp = res.data.datas;
					for (let i = 0, j = res.data.datas.length; i < j; i++) {
						//奇偶性
						temp[i].JO = i % 2 == 0 ? 'O' : 'J'
						temp[i].Youhui_Image = temp[i].P_Sale_Money == '0.00' ? '../../images/w2/temp8.png' : '../../images/w/63.png'
						temp[i].Price1 = temp[i].P_Price
						temp[i].Price2 = temp[i].P_Sale_Money == '0.00' ? '0.00' : temp[i].P_YMoney
					}
					console.log(temp)
					_this.setData({
						HomeProductList: temp
					});
				}
			},
			complete: function () {
				wx.hideNavigationBarLoading()
			}
		})
	},

	/**
	 * 点击事件————首页四个按钮
	 */
	btnEventHomeButton: function (res) {

		var index = res.currentTarget.dataset.index;
		var url = res.currentTarget.dataset.url;
		// var pageList = ['Fl_PinPai', 'Fl_FuWu', 'Fl_ZhaoShang', 'Fl_GouMai'];
		var name = res.currentTarget.dataset.name;
		wx.navigateTo({
			url: '../Fl_PinPai/index?id=' + url+'&name='+name,
		})
	},

	/**
	 * 网络请求--搜索
	 */
	requestNetHomeSearch: function (res) {
		var _this = this;
		var value = res.detail.value;
		wx.showNavigationBarLoading();
		wx.request({
			url: 'https://www.chnode.xyz/api/Home.ashx?DataType=Home_Search',
			data: {
				Search: value,
				AgentSaleID: getApp().data.AS_ID,
				AgentID: getApp().data.A_ID,
			},
			success: function (res) {
				wx.hideNavigationBarLoading()
				if (res.data.isok == 'true' && res.data.datas.length > 0) {
					wx.navigateTo({
						url: '../Fl_Search/index?key=' + value + '&datas=' + JSON.stringify(res.data.datas),
					})
				} else {
					CommentUtils.showToastWithImg("未找到商品", 3)
				}
			},
			fail: function (res) {
				wx.hideNavigationBarLoading()
				CommentUtils.showToastWithImg("请稍后尝试", 3)
			},
			complete: function (res) { wx.hideNavigationBarLoading() }
		})
	},

	/**
	 * 网络请求--绑定用户信息
	 */
	requestNetUInfoInsert: function (param1, param2, param3, param4, param5, param6) {
		var _this = this;
		wx.request({
			url: 'https://www.chnode.xyz/api/My.ashx?DataType=UInfoInsert',
			data: {
				Openid: getApp().data.openid,
				Country: param1,
				Privince: param2,
				City: param3,
				HeadimgUrl: param4,
				NickName: param5,
				Sex: param6,
			},
			success: function (res) {
				wx.setStorageSync("HasUserInfo", true)
				_this.setData({
					hasUserInfo: true
				})
			}
		})
	},

	/**
	 * 点击事件--扫描二维码
	 */
	btnEventScan: function (res) {
		var _this = this;
		wx.scanCode({
			onlyFromCamera: false,
			scanType: 'qrCode',
			success: function (res) {
				var result = JSON.parse(res.result)
				if (result.Type == '促销活动') {
					if (result.AS_ID != '' && result.A_ID != '') {
						getApp().data.AS_ID = result.AS_ID
						getApp().data.A_ID = result.A_ID
						_this.requestNetHomeProductList();
					}
				} else if (result.Type == '充值卡') {
					console.log("充值卡");
					if (result.R_Key != '' && result.R_Code != '' && result.R_Money != '') {
						var str = JSON.stringify({ "R_Code": result.R_Code, "R_Key": result.R_Key, "R_Money": result.R_Money });
						wx.navigateTo({
							url: '../W_ChongZhi/index?str=' + str,
						})
					}

				}
			}
		})
	},

	/**
	 * 点击事件--获取个人信息
	 */
	btnEventUserInfoGet: function (e) {
		if (e.detail.userInfo) {
			getApp().globalData.userInfo = e.detail.userInfo
			this.requestNetUInfoInsert(
				e.detail.userInfo.country,
				e.detail.userInfo.province,
				e.detail.userInfo.city,
				e.detail.userInfo.avatarUrl,
				e.detail.userInfo.nickName,
				e.detail.userInfo.gender)

		}
	},

	/**
	 * 点击事件————订单列表
	 */
	btnEventHomeProductList: function (res) {
		wx.navigateTo({
			url: "/pages/W_ProductDetail/index?pid=" + res.currentTarget.dataset.p_id+'&share=0'
		})
	}
})